package com.hd.crdao.fragment;

import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.activity.NewLoanCarActivity;
import com.hd.crdao.adapter.NewLoanCarRVAdapter;
import com.hd.crdao.base.BaseFragment;
import com.hd.crdao.bean.LoanCarListBean;
import com.hd.crdao.config.Config;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;

/**
 * @author Programming
 * @date 2017/10/30 0030
 */

public class LoancarChangeFragment extends BaseFragment  {
    public Handler mHandler = new Handler();
    public RecyclerView mRecyclerView;
    public RefreshLayout mRefreshLayout;
    public NewLoanCarRVAdapter mNewLoanCarRVAdapter;

    public List<LoanCarListBean.RowsBean> mFinalRowsBeanList = new ArrayList<>();
    public int pageNo = 1;
    public int pageSize = 4;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_loancar;
    }

    @Override
    protected void initView() {
        mRecyclerView = (RecyclerView) mViewRoot.findViewById(R.id.loanCarRecyclerview);
        mRefreshLayout = (RefreshLayout) mViewRoot.findViewById(R.id.refreshLayout);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    }




    @Override
    protected void initListener() {
        mRefreshLayout.autoRefresh();

        mRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageNo += 1;
                loadData(pageNo);
                refreshlayout.finishLoadmore();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                initData();
                refreshlayout.finishRefresh();
            }
        });
    }

    @Override
    protected void initData() {
        mFinalRowsBeanList.clear();
        pageNo=1;
        loadData(pageNo);
        mRefreshLayout.setLoadmoreFinished(false);
    }


    protected void loadData(int pageNo) {
        //访问网络---获取数据---设置数据
        String businessId = SPUtils.getString(Config.businessId, "");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("data", businessId + "~2");
        hashMap.put("sign", Constants.SIGN);
        hashMap.put("pageSize", String.valueOf(pageSize));
        hashMap.put("pageNo", String.valueOf(pageNo));

        OkHttpUtils.post().url(Constants.LOANCAR_URL).params(hashMap).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
                LoanCarListBean loanCarListBean = new Gson().fromJson(response, LoanCarListBean.class);
                if (loanCarListBean.getSuccess() == 0) {
                    List<LoanCarListBean.RowsBean> rows = loanCarListBean.getRows();
                    ALog.d(rows.size() + "");
                    mFinalRowsBeanList.addAll(rows);
                    mNewLoanCarRVAdapter = new NewLoanCarRVAdapter(getActivity(), mFinalRowsBeanList,loanCarListBean);
                    mRecyclerView.setAdapter(mNewLoanCarRVAdapter);
                    ((NewLoanCarActivity)getActivity()).setNotice(loanCarListBean.getCreditTotal(),
                            loanCarListBean.getAuditTotal(),
                            loanCarListBean.getFinishTotal()
                            );
                    //((NewLoanCarActivity)getActivity()).setParams(2,loanCarListBean.getCreditTotal()+"");

                    //  mRecyclerView.smoothScrollToPosition(mFinalRowsBeanList.size()- pageSize);
                    if (loanCarListBean.getTotal() <= mFinalRowsBeanList.size()) {
                        mRefreshLayout.setLoadmoreFinished(true);
                    }

                }
            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()&&mRefreshLayout!=null) {
           // initData();
            mRefreshLayout.autoRefresh();
            ALog.d("用户可见时,加载数据");
        }

    }
/**/
    @Override
    public void onStart() {
        super.onStart();
       // initData();
        mRefreshLayout.autoRefresh();
    }
}
